﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Globalization;
using MySql.Data.MySqlClient;

namespace PHP
{
	public partial class dto : Form
	{
		public dto()
		{
			InitializeComponent();

		}
		private FormSetingStore fs;
		private bool kiemTra()
		{
			for (int i = 0; i < dg.RowCount - 1; i++)
			{
				for (int j = i + 1; j < dg.RowCount; j++)
				{
					if (dg["name", i].Value.Equals(dg["name", j].Value))
					{
						MessageBox.Show("Lỗi lặp lại");
						dg.CurrentCell = dg.Rows[j].Cells[1];
						dg.BeginEdit(true);



						return false;
					}
				}
			}
			return true;
		}
		private string vietHoa(string str)
		{
			//var ci = new CultureInfo("vi-VI");
			string tmp = "";
			char[] s = str.ToArray();
			if (s.Length > 0)
			{
				s[0] = char.ToUpper(s[0]);
			}
			foreach (var i in s)
			{
				tmp += i;
			}
			return tmp.ToString();
		}
		private void btToaCode_Click(object sender, EventArgs e)
		{

			if (!kiemTra()) return;// nếu sai thì dừng lại không làm nữa

			int n = dg.RowCount - 1;
			string str = "<?PHP";

			str += "\nclass " + vietHoa(txClass.Text.Trim()) + "{";
			str += "\n";
			for (int i = 0; i < n; i++)
			{
				//các biến
				str += "\t private $" + dg["name", i].Value.ToString().Trim() + ";";
				str += "\n";
			}
			str += "\n";
			//construct
			if (ckConstruct.Checked)
			{
				str += "\tpublic function __construct(";
				for (int i = 0; i < n - 1; i++)
				{
					str += "$" + dg["name", i].Value.ToString() + "=\"\",";
				}
				str += "$" + dg["name", n - 1].Value.ToString() + "=\"\"){";
				for (int i = 0; i < n; i++)
				{
					str += "\n\t\t$this->" + dg["name", i].Value.ToString().Trim() + "=" + "$" + dg["name", i].Value.ToString().Trim() + ";";
				}
				str += "\n\t}";
			}
			str += "\n";

			// JSon
			if (ckGetJson.Checked)
			{
				str += "\tpublic function getJson(){";
				str += "\n\t\t$item = array(";
				for (int i = 0; i < n - 1; i++)
				{
					str += "\n\t\t\t'" + dg["name", i].Value.ToString().Trim() + "'=>$this->" + dg["name", i].Value.ToString().Trim() + ",";
				}
				str += "\n\t\t\t'" + dg["name", n - 1].Value.ToString().Trim() + "'=>$this->" + dg["name", n - 1].Value.ToString().Trim();
				str += "\n\t\t);";
				str += "\n\t\treturn $item;";
				str += "\n\t}";
			}
			str += "\n";
			// hàm set
			for (int i = 0; i < n; i++)
			{
				if (dg["Set", i].Value != null)
				{
					if (bool.Parse(dg["Set", i].Value.ToString()))
					{
						str += "\tpublic function Set" + vietHoa(dg["name", i].Value.ToString().Trim()) + "($" + dg["name", i].Value.ToString().Trim() + "){";
						str += "\n\t\t $this->" + dg["name", i].Value.ToString().Trim() + " = $" + dg["name", i].Value.ToString().Trim() + ";";
						str += "\n\t}\n";
					}
				}
			}

			str += "\n";

			// hàm get
			for (int i = 0; i < n; i++)
			{
				if (dg["Set", i].Value != null)
				{
					if (bool.Parse(dg["Get", i].Value.ToString()))
					{
						str += "\n\tpublic function Get" + vietHoa(dg["name", i].Value.ToString().Trim()) + "(){";
						str += "\n\t\t return $this->" + dg["name", i].Value.ToString().Trim() + ";";
						str += "\n\t}";
					}
				}
			}
			//cuối class
			str += "\n}";

			//MODLE


			str += "\nclass " + vietHoa(txClass.Text.Trim()) + "Model{";
			if (ckInsert.Checked)
			{

				str += "\n\tpublic function Insert($" + vietHoa(txClass.Text.Trim()) + "){";
				// sua lai chuoi insert

				//    $sql = "INSERT INTO `bdwal`.`bdbang` (`MaBang`, `FlagMD5`, `TenBang`) VALUES (\'2\', \'hai\', \'ten\');";

				//    $sqlInsert = "insert into results(dateCreate,madai,tinhId,DB,giai_I,giai_II";
				//$sqlInsert.=",giai_III,giai_IV,giai_V,giai_VI,giai_VII,giai_VIII,status) values('" ;
				//$_madai = ""; 

				str += "\n\t\t\t$mysql = \"INSERT INTO 'bdwal'.'" + vietHoa(txClass.Text) + "' (";
				//ten cac cot
				for (int i = 0; i < n - 1; i++)
				{
					str += "'" + vietHoa(dg["name", i].Value.ToString() )+ "',";
				}
				str += "\'" + vietHoa(dg["name", n - 1].Value.ToString()) + "\'";

				str += ")\";";
				//value
				str += "\n\t\t\t$mysql.=\"VALUES(\";";
				for (int i = 0; i < n - 1; i++)
				{
					str += "\n\t\t\t$mysql.=$" + vietHoa(txClass.Text) + "->" + dg["name", i].Value.ToString() + ".\",\";";
				}
				str += "\n\t\t\t$mysql.=$" + vietHoa(txClass.Text) + "->" + dg["name", n - 1].Value.ToString();

				str += ".\");\";";

				str += "\n\t\t\tDataProvider :: ExecuteQuery($mysql);";

				str += "\n\t}";
			}
			if (ckEdit.Checked)
			{
				str += "\n\tpublic function Edit($" + vietHoa(txClass.Text.Trim()) + "){";
				// sua lai ham edit
				str += "\n\t}";
			}
			if (ckDelete.Checked)
			{
				str += "\n\tpublic function Delete($" + vietHoa(txClass.Text.Trim()) + "){";

				str += "\n\t}";

			}

			// Hien thi thong tin 1 ob
			if (ckGetByID.Checked)
			{
				str += "\n\tpublic function Get" + vietHoa(txClass.Text) + "ById($" + dg["name", 0].Value.ToString().Trim() + "){";
				str += "\n\t\t$mysql = \"select * from " + vietHoa(txClass.Text) + " where " + dg["name", 0].Value.ToString().Trim() + " = \"$" + dg["name", 0].Value.ToString().Trim() + ";";
				str += "\n\t\t$results = DataProvider::ExecuteQuery($mysql);";
				str += "\n\t\t$data = array();";
				str += "\n\t\tif($results != FALSE){";
				str += "\n\t\t\twhile ($row = mysql_fetch_array($results,MYSQL_ASSOC)){";
				str += "\n\t\t\t\t$item = new " + vietHoa(txClass.Text.Trim()) + " (";
				//cac bien trong class
				for (int i = 0; i < n; i++)
				{
					if (i != n - 1) str += " $row[" + dg["name", i].Value.ToString().Trim() + "],";
					else str += " $row[" + dg["name", i].Value.ToString().Trim() + "]";
				}
				str += ");";
				str += "\n\t\t\t\t$data[] = $item->getJson();";
				str += "\n\t\t\t}";
				str += "\n\t\t}";
				str += "\n\t\treturn $data;";
				str += "\n\t}";
			}
			//lay tat ca danh sach users
			str += "\n\tpublic function GetAll" + vietHoa(txClass.Text.Trim()) + "(){";
			str += "\n\t\t$mysql = \"select * from " + vietHoa(txClass.Text.Trim()) + ";\";";
			str += "\n\t\t$results = DataProvider::ExecuteQuery($mysql);";
			str += "\n\t\t$data = array();";
			str += "\n\t\tif($results != FALSE){";
			str += "\n\t\t\twhile ($row = mysql_fetch_array($results,MYSQL_ASSOC)){";
			str += "\n\t\t\t\t$item = new " + vietHoa(txClass.Text.Trim()) + " (";
			//cac bien trong class
			for (int i = 0; i < n; i++)
			{
				if (i != n - 1) str += " $row[" + dg["name", i].Value.ToString().Trim() + "],";
				else str += " $row[" + dg["name", i].Value.ToString().Trim() + "]";
			}
			str += ");";
			str += "\n\t\t\t\t$data[] = $item->getJson();";
			str += "\n\t\t\t}";
			str += "\n\t\t}";
			str += "\n\t\treturn $data;";
			str += "\n\t}";
			str += "\n}";
			//END MODLE
			str += "\n?>";
			txt1.Text = str.ToString();
		}

		private void btCopy_Click(object sender, EventArgs e)
		{
			Clipboard.SetText(txt1.Text);
		}

		private void dg_CellEndEdit(object sender, DataGridViewCellEventArgs e)
		{
			int row = dg.CurrentCell.RowIndex;
			dg[0, row].Value = row + 1;
			//set
			dg["Set", row].Value = true;
			//get
			dg[3, row].Value = true;
		}

		private void ckModel_CheckedChanged(object sender, EventArgs e)
		{

			foreach (var item in groupBox1.Controls)
			{
				((CheckBox)item).Checked = ckModel.Checked;
			}

		}

		private void dto_Load(object sender, EventArgs e)
		{
			//ham();

			fs = new FormSetingStore(@"SOFTWARE\PHP\Form\1.0", this.Name);
			fs.ApplySetings(this);

			txClass.Focus();
			return;
		}


		private void dto_FormClosing(object sender, FormClosingEventArgs e)
		{
			fs.SaveSettings(this);
		}
		private List<string> getTableName()
		{
			List<string> l = new List<string>();
			StringBuilder st = new StringBuilder();
			try
			{
				Console.WriteLine("Connecting to MySQL...");
				//	MessageBox.Show("Connecting to MySQL...");
				string sql = @"SHOW FULL TABLES IN BDWAL";

				MySqlCommand cmd = new MySqlCommand(sql, kn());
				MySqlDataReader rdr = cmd.ExecuteReader();

				while (rdr.Read())
				{
				//	Console.WriteLine(rdr[0]);
					string str = rdr[0].ToString();
					l.Add(str);
				// + " -- " + rdr[1]);
				}
				rdr.Close();

				// Perform database operations
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				//	MessageBox.Show(ex.Message.ToString());
			}
			kn().Close();
			//MessageBox.Show("Done");
			Console.WriteLine("Done.");
			return l;
		}
		private MySqlConnection kn()
		{
			string connStr = "server=localhost;user=root;database=bdwal;port=3306;password=\"\";";
			MySqlConnection conn = new MySqlConnection(connStr);
			conn.Open();
			return conn;
		}
		private void ham()
		{

			StringBuilder st = new StringBuilder();
			try
			{
				Console.WriteLine("Connecting to MySQL...");
				//	MessageBox.Show("Connecting to MySQL...");
				string sql = @"SHOW FULL TABLES IN BDWAL";

				MySqlCommand cmd = new MySqlCommand(sql, kn());
				MySqlDataReader rdr = cmd.ExecuteReader();

				while (rdr.Read())
				{
					Console.WriteLine(rdr[0] + " -- " + rdr[1]);
				}
				rdr.Close();

				// Perform database operations
			}
			catch (Exception ex)
			{
				Console.WriteLine(ex.ToString());
				//	MessageBox.Show(ex.Message.ToString());
			}
			kn().Close();
			//MessageBox.Show("Done");
			Console.WriteLine("Done.");
		}

		private List<Table> getTable()
		{
			List<Table> ltb= new List<Table>();

			foreach (var item in getTableName())
			{
				//lay ten cot va kieu du lieu
				string sql = @"SHOW COLUMNS FROM "+ item.ToString();

				MySqlCommand cmd = new MySqlCommand(sql, kn());
				MySqlDataReader rdr = cmd.ExecuteReader();
				Table tb = new Table();
				tb.nameTable = item.ToString();
				while (rdr.Read())
				{
					//	Console.WriteLine(rdr[0]);

					tb.nameColumn.Add(rdr[0].ToString());
					tb.typeOfCoumn.Add(rdr[1].ToString());
				//	string str = rdr[0].ToString();
					ltb.Add(tb);
					// + " -- " + rdr[1]);
				}
			}

			return ltb;

		}
		private void button1_Click(object sender, EventArgs e)
		{
			//string str = "";
			//foreach (var item in getTableName())
			//{
			//       str += item.ToString() + "\n";
			//}

			//txt1.Text = str.ToString();

			object ob = getTable();

		}
	}
}